home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir39 / borfix.zip / TI1300.ASC < prev    next >
Text File  |  1993-10-25  |  9KB  |  397 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1300
  9.   VERSION  :  3.x
  10.        OS  :  DOS
  11.      DATE  :  October 25, 1993                         PAGE  :  1/6
  12.  
  13.     TITLE  :  Example of how to access expanded (EMS) memory.
  14.  
  15.  
  16.  
  17.  
  18.  
  19.   /************************************************************
  20.     This program demonstrates the use of EMS (expanded memory.)
  21.     In this program, we use EMS to store a simulated array of
  22.     50,000 long ints.  Compile in any memory model.
  23.     It is not optimized, for purposes of clarity.
  24.     Many thanks to Ray Duncan for his lucid exposition of
  25.     EMS in "Extending DOS"
  26.    ************************************************************/
  27.  
  28.   #include <fcntl.h>
  29.   #include <sys\stat.h>
  30.   #include <io.h>
  31.   #include <stdio.h>
  32.   #include <dos.h>
  33.   #include <stdlib.h>
  34.  
  35.   #define TRUE 1
  36.   #define FALSE 0
  37.   #define EMS_INT 0x67
  38.   #define PAGESIZE 16384
  39.  
  40.   int EMSHandle;
  41.   int pageMap[4] = { -1, -1, -1, -1 };
  42.   int pageFrame;
  43.  
  44.   // Function to test for existence of EMS.  If coding a TSR or
  45.   // ISR, you will be unable to call DOS and therefore must check
  46.   // if the name EMMXXXX0 is at offset 10 from the entrypoint to
  47.   // the EM Manager pointed to by vector 0x67
  48.  
  49.   char EMSExists(void)
  50.   {
  51.        int handle,devInfo,outputStatus;
  52.        // The expanded memory manager if present should show up
  53.        // as a filename
  54.        if ((handle=open("EMMXXXX0",O_RDONLY|O_BINARY)) == -1)
  55.             return FALSE;
  56.        devInfo=ioctl(handle,0);
  57.        // if bit 7 is 0 it is a file
  58.        if ((devInfo & 0x80) == 0)
  59.        {
  60.             printf("Found File EMMXXXX0?!?\n");
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1300
  75.   VERSION  :  3.x
  76.        OS  :  DOS
  77.      DATE  :  October 25, 1993                         PAGE  :  2/6
  78.  
  79.     TITLE  :  Example of how to access expanded (EMS) memory.
  80.  
  81.  
  82.  
  83.  
  84.             close(handle);
  85.             return FALSE;
  86.        }
  87.        outputStatus=ioctl(handle,7);
  88.        if (outputStatus==0)
  89.        {
  90.             printf("EMM present but not responding\n");
  91.             close(handle);
  92.             return FALSE;
  93.        }
  94.        // file handle for EMS manager is useless for anything
  95.        // further, so we'll just close it.
  96.        close(handle);
  97.        return TRUE;
  98.   }
  99.  
  100.   // Assuming EMS is present, it may still not be functional
  101.   // due to a hardware error or who knows what, so we have
  102.   // a function to check whether the expanded memory manager
  103.   // is ready to do its business.
  104.  
  105.   char EMSFunctional(void)
  106.   {
  107.        _AH=0x40;
  108.        geninterrupt(EMS_INT);
  109.        if (_AH==0) return FALSE;
  110.        else return TRUE;
  111.   }
  112.  
  113.   // EMSSize() finds out the total and available pages of EMS
  114.  
  115.   void EMSSize(int *pagesTotal, int *pagesAvail)
  116.   {
  117.       _AH=0x42;
  118.       geninterrupt(0x67);
  119.       *pagesAvail=_BX;
  120.       *pagesTotal=_DX;
  121.   }
  122.  
  123.   // EMSAlloc() tries to allocate the number of pages corresponding
  124.   // to the size in bytes of the allocation requested.
  125.  
  126.   char EMSAlloc(unsigned long bytes)
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1300
  141.   VERSION  :  3.x
  142.        OS  :  DOS
  143.      DATE  :  October 25, 1993                         PAGE  :  3/6
  144.  
  145.     TITLE  :  Example of how to access expanded (EMS) memory.
  146.  
  147.  
  148.  
  149.  
  150.   {
  151.        int numPages;
  152.        numPages=(int) (bytes/PAGESIZE);
  153.        if (bytes%PAGESIZE != 0) ++numPages;
  154.        _BX=numPages;
  155.        _AH=0x43;
  156.        geninterrupt(EMS_INT);
  157.        EMSHandle=_DX;
  158.        if (_AH!=0)
  159.        {
  160.             printf("Allocation of %d pages failed\n",numPages);
  161.             return FALSE;
  162.        }
  163.        else
  164.        {
  165.             printf("Allocated %d pages\n",numPages);
  166.             return TRUE;
  167.        }
  168.   }
  169.  
  170.   // CleanUp() tries to deallocate our EMS allocation
  171.  
  172.   void CleanUp(void)
  173.   {
  174.        _AH=0x45;
  175.        _DX=EMSHandle;
  176.        geninterrupt(EMS_INT);
  177.        if (_AH!=0) printf("Deallocation failed!\n");
  178.   }
  179.  
  180.   void EMSRemap(int logpage)
  181.   {
  182.        // the mapping scheme we're using is that the physical page
  183.        // mapping for a logical page is the logical page mod 4
  184.        printf("Mapping logical page %d\n",logpage);
  185.        // ask to map logical page _BX into physical page (0->3) _AL
  186.        _AL=logpage&3;
  187.        _BX=logpage;
  188.        _DX=EMSHandle;
  189.        _AH=0x44;
  190.        geninterrupt(EMS_INT);
  191.        if (_AH!=0)
  192.        {
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1300
  207.   VERSION  :  3.x
  208.        OS  :  DOS
  209.      DATE  :  October 25, 1993                         PAGE  :  4/6
  210.  
  211.     TITLE  :  Example of how to access expanded (EMS) memory.
  212.  
  213.  
  214.  
  215.  
  216.             printf("Mapping failed!\n");
  217.             CleanUp();
  218.             exit(1);
  219.        }
  220.        pageMap[logpage&3]=logpage;
  221.   }
  222.  
  223.   // GetEMSPageFrame finds the segment that the page frame lives
  224.   // at and stores it in pageFrame for use by PutVal and GetVal
  225.  
  226.   void GetEMSPageFrame(void)
  227.   {
  228.        _AH=0x41;
  229.        geninterrupt(EMS_INT);
  230.        pageFrame=_BX;
  231.        if (_AH!=0)
  232.        {
  233.             printf("Cannot get page frame!\n");
  234.             CleanUp();
  235.             exit(1);
  236.        }
  237.   }
  238.  
  239.  
  240.   // PutVal() puts a value into the array, asking for a page to be
  241.   // remapped if the correct page isn't in memory.
  242.  
  243.   // With both PutVal() and GetVal(), the code could be optimized
  244.   // considerably by using bitshifts instead of multiplies.  And,
  245.   // a real application would probably be coded to check less
  246.   // often for the page being present.
  247.  
  248.   void PutVal(unsigned long loc, unsigned long val)
  249.   {
  250.        unsigned int logPage; // logical page
  251.        unsigned long far * physLoc;
  252.        // loc*sizeof(long) is the byte address of the item; we
  253.        // divide by PAGESIZE to figure out which logical page
  254.        // it's on
  255.        logPage= (int) (loc*sizeof(long)/PAGESIZE);
  256.        // logical page mod 4 is our scheme to map a logical page
  257.        // to a physical page.   pageMap tells us which logical
  258.        // page is being kept in each physical page.
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland C++                           NUMBER  :  1300
  273.   VERSION  :  3.x
  274.        OS  :  DOS
  275.      DATE  :  October 25, 1993                         PAGE  :  5/6
  276.  
  277.     TITLE  :  Example of how to access expanded (EMS) memory.
  278.  
  279.  
  280.  
  281.  
  282.        if (pageMap[logPage&3]!=logPage) EMSRemap(logPage);
  283.        // the offset in the EMS buffer will be sizeof(long) times
  284.        // the array index, mod 64K; the segment of the EMS buffer
  285.        // is of course pageFrame.
  286.        physLoc= (unsigned long far *)
  287.             MK_FP(pageFrame,(loc*sizeof(long))&65535U);
  288.        *physLoc=val;
  289.   }
  290.  
  291.   // GetVal() gets a value from the 'array', asking for a page to
  292.   // be remapped if the correct page isn't in memory.
  293.  
  294.   unsigned long GetVal(unsigned long loc)
  295.   {
  296.        unsigned int logPage;
  297.        unsigned long far * physLoc;
  298.        logPage= (int) (loc*sizeof(long)/PAGESIZE);
  299.        if (pageMap[logPage&3]!=logPage) EMSRemap(logPage);
  300.        physLoc= (unsigned long far *)
  301.             MK_FP(pageFrame,(loc*sizeof(long))&65535U);
  302.        return *physLoc;
  303.   }
  304.  
  305.   int main(void)
  306.   {
  307.        int pagesAvail,pagesTotal;
  308.        unsigned long i;
  309.        if (EMSExists()) printf("Found EMS\n");
  310.        else
  311.        {
  312.             printf("No EMS");
  313.             return(1);
  314.        }
  315.        if (EMSFunctional) printf("EMS Working\n");
  316.        else
  317.        {
  318.             printf("EMS present but malfunctioning\n");
  319.             return(1);
  320.        }
  321.        EMSSize(&pagesTotal,&pagesAvail);
  322.        printf("EMS Pages -- Total: %u, Available %u (%uK)\n",
  323.                  pagesTotal,pagesAvail,pagesAvail*16);
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.   PRODUCT  :  Borland C++                           NUMBER  :  1300
  339.   VERSION  :  3.x
  340.        OS  :  DOS
  341.      DATE  :  October 25, 1993                         PAGE  :  6/6
  342.  
  343.     TITLE  :  Example of how to access expanded (EMS) memory.
  344.  
  345.  
  346.  
  347.  
  348.        // allocate enough space in EMS for 50,000 long ints
  349.        if (!EMSAlloc(sizeof(long)*50000L))
  350.             return(1);
  351.  
  352.        GetEMSPageFrame();
  353.        printf("Page frame at segment 0x%4X\n",pageFrame);
  354.  
  355.        // Test our EMS functions by storing and retrieving
  356.        // 50,000 long ints
  357.        for (i=0; i<50000L; ++i)
  358.             PutVal(i,i);
  359.        for (i=0; i<50000L; ++i)
  360.             if (GetVal(i)!=i)
  361.             {
  362.                  printf("%d != %d\n",GetVal(i),i);
  363.                  break;
  364.             }
  365.  
  366.        CleanUp();
  367.        return(0);
  368.   }
  369.  
  370.  
  371.   DISCLAIMER: You have the right to use this technical information
  372.   subject to the terms of the No-Nonsense License Statement that
  373.   you received with the Borland product to which this information
  374.   pertains.
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.